package com.train.user.controller;

import com.train.common.core.controller.BaseController;
import com.train.common.core.domain.AjaxResult;
import com.train.common.core.domain.model.LoginUser;
import com.train.common.utils.SecurityUtils;
import com.train.user.dto.CommentDTO;
import com.train.user.service.SysPostCommentService;
import com.train.user.vo.SysPostCommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 动态评论管理 控制器
 * 
 * @author train
 */
@RestController
@RequestMapping("/user/post/comment")
public class SysPostCommentController extends BaseController {

    @Autowired
    private SysPostCommentService sysPostCommentService;

    /**
     * 发布评论
     * parentId为0或null表示主评论,有值表示回复评论
     * 
     * @param commentDTO 评论信息
     * @return 结果
     */
    @PostMapping
    public AjaxResult addComment(@Validated @RequestBody CommentDTO commentDTO) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        
        boolean result = sysPostCommentService.addComment(userId, commentDTO);
        if (result) {
            if (commentDTO.getParentId() != null && commentDTO.getParentId() > 0) {
                return success("回复成功");
            } else {
                return success("评论成功");
            }
        }
        return error("评论失败");
    }

    /**
     * 删除评论
     * 
     * @param commentId 评论ID
     * @return 结果
     */
    @DeleteMapping("/{commentId}")
    public AjaxResult deleteComment(@PathVariable("commentId") Long commentId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        
        boolean result = sysPostCommentService.deleteComment(userId, commentId);
        return result ? success("删除成功") : error("删除失败或无权限");
    }

    /**
     * 获取动态的评论列表(两级结构)
     * 
     * @param postId 动态ID
     * @return 评论列表
     */
    @GetMapping("/list/{postId}")
    public AjaxResult getCommentList(@PathVariable("postId") Long postId) {
        List<SysPostCommentVO> commentList = sysPostCommentService.getCommentListByPostId(postId);
        return success(commentList);
    }

    /**
     * 获取评论详情
     * 
     * @param commentId 评论ID
     * @return 评论详情
     */
    @GetMapping("/{commentId}")
    public AjaxResult getCommentDetail(@PathVariable("commentId") Long commentId) {
        SysPostCommentVO comment = sysPostCommentService.getCommentDetail(commentId);
        return success(comment);
    }

    /**
     * 点赞评论
     *
     * @param commentId 评论ID
     * @return 结果
     */
    @PostMapping("/{commentId}/like")
    public AjaxResult likeComment(@PathVariable("commentId") Long commentId) {
        boolean result = sysPostCommentService.likeComment(commentId);
        return result ? success("点赞成功") : error("点赞失败或已点赞");
    }

    /**
     * 取消点赞评论
     *
     * @param commentId 评论ID
     * @return 结果
     */
    @DeleteMapping("/{commentId}/like")
    public AjaxResult unlikeComment(@PathVariable("commentId") Long commentId) {
        boolean result = sysPostCommentService.unlikeComment(commentId);
        return result ? success("取消点赞成功") : error("取消点赞失败或未点赞");
    }

    /**
     * 检查当前用户是否已点赞该评论
     *
     * @param commentId 评论ID
     * @return 是否已点赞(true/false)
     */
    @GetMapping("/{commentId}/liked")
    public AjaxResult isLiked(@PathVariable("commentId") Long commentId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        boolean liked = sysPostCommentService.isCommentLiked(userId, commentId);
        return success(liked);
    }

}

